<?php


class Util
{

    /**
     * 概率算法
     * @param $proArr  string|array 4:40%|5:50%|6:10% 或者  array('4'=>'40%','5'=>'50%','6'=>'10%')
     */
    static function getRandData($proArr,$random = false)
    {
        //不是数组转为数组
       if (!is_array($proArr)){
              $proArr = explode('|',$proArr);
              $arr = [];
              foreach ($proArr as $item){
                $item = explode(':',$item);
                $arr[$item[0]] = $item[1];
              }
              $proArr = $arr;
       }
       $data = [];
       foreach ($proArr as $key => $val) {
           //$val去除%号
           $len = str_replace('%','',$val);
           $item = array_fill(0, $len, $key);
           $data = array_merge($data,$item);
       }
       //将数组打乱
       shuffle($data);
       if ($random){
           return $data[mt_rand(0, count($data) - 1)];
       }else{
           return $data;
       }
    }

    /**
     * hashids加密
     */
    static function hashids_encode($id)
    {
        $hashids = new \Hashids\Hashids('hashids', 6);
        return $hashids->encode($id);
    }

    /**
     * hashids解密
     */
    static function hashids_decode($id){
        $hashids = new \Hashids\Hashids('hashids', 6);
        $ids =  $hashids->decode($id);
        return count($ids)>0?$ids[0]:false;
    }

    /**
     * 倒数计时显示
     * @param $countdown
     * @return string
     */
    static function countdown($countdown){
        //计算到期文本显示时间
        $hour = ($countdown/60/60)%24;
        //$hour不足两位补0
        $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
        $minute = ($countdown/60)%60;
        $minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
        $second = $countdown%60;
        $second = str_pad($second, 2, '0', STR_PAD_LEFT);
        $countdown_text = $hour . ':' . $minute . ':' . $second;
        return $countdown_text;
    }

    //生成唯一订单号
    static function makeSn($prefix=''){
        //取微秒的格式化时间
        return $prefix.date('YmdHis').substr(microtime(),2,6);
    }

    /**
     * 获取平台
     * @return string
     */
    static function getFlatform(){
        //判断H5、Android、ios、PC
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone')||strpos($agent, 'ipad')){
            return 'ios';
        }else if(strpos($agent, 'android')){
            return 'android';
        }else if(strpos($agent, 'windows')) {
            return 'pc';
        }else{
            return 'h5';
        }
    }

    /**
     * 获取顶级域名
     * @param $domain
     * @return void
     */
    static function getTopDomain($domain){
        if (strpos($domain,'.') !== false){
            $domain = explode('.',$domain);
            $domain = $domain[count($domain)-2].'.'.$domain[count($domain)-1];
        }
        return $domain;
    }

    /**
     * 生成订单
     * @param $prefix
     * @return string
     */
    static function makeOrderNo($prefix=''){
        return $prefix.date('YmdHis').rand(1000,9999);
    }

    /**
     * 对象转数组
     * @param $object
     * @return mixed
     */
    static function objectToArray($object){
        return json_decode(json_encode($object),true);
    }

    /**
     * 数组第一条数据
     * @param $array
     * @param $key
     * @return mixed|null
     */
    static function  arrayFirst($array,$key){
        if (!$key){
            return count($array)>0?$array[0]:null;
        }else{
            return count($array)>0?$array[0][$key]:null;
        }

    }

    /**
     * 判断是否是手机号
     * @param $mobile
     * @return string
     */
     static  public function  isMobile($mobile) {
         $regex = "/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/";
         $isMatched = preg_match_all($regex, $mobile);
         return $isMatched;
    }


    /**
     * 获取客户端IP地址
     * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @param boolean $adv 是否进行高级模式获取（有可能被伪装）
     * @return mixed
     */
    static function get_client_ip($type = 0,$adv=false) {
        $type       =  $type ? 1 : 0;
        static $ip  =   NULL;
        if ($ip !== NULL) return $ip[$type];
        if($adv){
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos    =   array_search('unknown',$arr);
                if(false !== $pos) unset($arr[$pos]);
                $ip     =   trim($arr[0]);
            }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip     =   $_SERVER['HTTP_CLIENT_IP'];
            }elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip     =   $_SERVER['REMOTE_ADDR'];
            }
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u",ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }



    static function replace_image_url($url,$domain=''){
        $domain = !empty($domain)?$domain:request()->domain();
        $content=str_replace('/uploads/',$domain.'/uploads/',$url);
        return $content;
    }

    /**
     * 替换文章图片为绝对域名
     * @param $content 内容
     * @param string $domain 替换成的域名
     * @return mixed
     */
    static function replace_url($content,$domain=''){
        $domain = !empty($domain)?$domain:request()->domain();
        $content=str_replace('src="/uploads/','src="'.$domain.'/uploads/',$content);
        return $content;
    }

    /**
     * 替换文章图片绝对域名为相对路径
     * @param $content 内容
     * @param string $domain 替换成的域名
     * @return mixed
     */
    static function remove_url($content,$domain=''){
        $domain = !empty($domain)?$domain:request()->domain();
        $content=str_replace('src="'.$domain.'/uploads/','src="/uploads/',$content);
        return $content;
    }


    /**
     * 是否是url地址
     * @param $url
     * @return bool
     */
    static function  is_url($url){
        if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
           return true;
        }else{
           return false;
        }
    }
}
